package com.zf.yichat.config;

import org.lionsoul.ip2region.xdb.Searcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * 离线IP转地区
 * @author xxx
 */

@Configuration
public class Ip2RegionConfig {

    @Value("${ip2Regin.db_file_path}")
    private String DB_DILE_PATH;

    private static final Logger log = LoggerFactory.getLogger(Ip2RegionConfig.class);

    @Bean("ip2region")
    public Searcher initSearch() {
        Searcher searcher = null;
        try {
            searcher = Searcher.newWithFileOnly(DB_DILE_PATH);
        } catch (IOException e) {
            log.error("init Ip2Region fail: dbfile not found", e);
            throw new RuntimeException(e);
        } catch (Exception ex) {
            log.error("init Ip2Region fail", ex);
            throw new RuntimeException(ex);
        }
        return searcher;
    }
}
